package com.snap.vseries.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.base.BaseFragment;
import com.snap.vseries.bean.ExpInfo;
import com.snap.vseries.service.OnSectionProcessListener;
import com.snap.vseries.ui.ExpRunningActivity;

import java.util.Locale;

import butterknife.BindView;

/**
 * 运行中实验程序
 */
@SuppressLint("ValidFragment")
public class RunningProgramFragment extends BaseFragment {
    private final ExpInfo expInfo;
    @BindView(R.id.sample_name_tv)
    TextView sampleNameTv;
    @BindView(R.id.programs_tv)
    TextView programsTv;
    @BindView(R.id.current_process_tv)
    TextView currentProcessTv;
    @BindView(R.id.margin_view)
    View marginView;
    private OnSectionProcessListener onSectionProcessListener = new OnSectionProcessListener() {
        @Override
        public void onSectionProcess(int runningTotalSection, int runningCycleInStage, boolean isCurrentStageCycle) {
            if (currentProcessTv != null) {
                currentProcessTv.post(new Runnable() {
                    @Override
                    public void run() {
                        String content = "";
                        if (isCurrentStageCycle) {
                            content = "Stage " + String.format(Locale.CHINA, "%02d", runningTotalSection) + " - " + runningCycleInStage + "th cycle";
                        } else {
                            content = "Stage " + String.format(Locale.CHINA, "%02d", runningTotalSection);
                        }
                        if (currentProcessTv != null && !currentProcessTv.getText().toString().equals(content) && !content.isEmpty()) {
                            currentProcessTv.setText(content);
                        }
                    }
                });
            }
        }
    };

    @SuppressLint("ValidFragment")
    public RunningProgramFragment(ExpInfo expInfo) {
        this.expInfo = expInfo;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    protected int layoutResId() {
        return R.layout.fragment_running_program;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (getActivity() != null && ((ExpRunningActivity) getActivity()).expController != null) {
            ((ExpRunningActivity) getActivity()).expController.setOnSectionProcessListener(null);
        }

    }

    private void initView() {
        if (getActivity() != null) {
            ((ExpRunningActivity) getActivity()).expController.setOnSectionProcessListener(onSectionProcessListener);
        }
        ProgramFragment programFragment = new ProgramFragment(expInfo);
        getChildFragmentManager().beginTransaction().add(R.id.container, programFragment).commitAllowingStateLoss();
        sampleNameTv.setText(expInfo.getSampleName() == null ? "N/A" : expInfo.getSampleName());
        programsTv.setText(expInfo.getTestProject());

    }

    public void initMargin(boolean b) {
        marginView.setVisibility(!b ? View.VISIBLE : View.GONE);
    }

    public void initProcess(int runningTotalSection, int runningCycleInStage, boolean isCurrentStageCycle) {
        String content = "";
        if (isCurrentStageCycle) {
            content = "Stage " + String.format(Locale.CHINA, "%02d", runningTotalSection) + " - " + runningCycleInStage + "th cycle";
        } else {
            content = "Stage " + String.format(Locale.CHINA, "%02d", runningTotalSection);
        }
        if (currentProcessTv != null && !currentProcessTv.getText().toString().equals(content) && !content.isEmpty()) {
            currentProcessTv.setText(content);
        }
    }
}